package com.li.kafka.wechat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.li.kafka.wechat.common.BaseResponseVO;
import com.li.kafka.wechat.conf.WechatTemplateProperties;
import com.li.kafka.wechat.service.WechatTemplateService;
import com.li.kafka.wechat.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/v1")
public class WechatTemplateController {
  @Autowired private WechatTemplateProperties properties;

  @Autowired private WechatTemplateService wechatTemplateService;

  @RequestMapping(value = "/template", method = RequestMethod.GET)
  public BaseResponseVO getTemplate() {
    WechatTemplateProperties.WechatTemplate wechatTemplate =
        wechatTemplateService.getWechatTemplate();
    Map<String, Object> result = Maps.newHashMap();
    result.put("templateId", wechatTemplate.getTemplateId());
    result.put("template", FileUtils.readFile2JsonArray(wechatTemplate.getTemplateFilePath()));
    return BaseResponseVO.success(result);
  }

  @RequestMapping(value = "/template/result", method = RequestMethod.GET)
  public BaseResponseVO templateStatistics(
      @RequestParam(value = "templateId", required = false) String templateId) {
    JSONObject jsonObject = wechatTemplateService.templateStatistics(templateId);
    return BaseResponseVO.success(jsonObject);
  }

  @RequestMapping(value = "/template/report", method = RequestMethod.POST)
  public BaseResponseVO dataReported(@RequestBody String reportData) {
    wechatTemplateService.templateReported(JSON.parseObject(reportData));
    return BaseResponseVO.success();
  }
}
